import { orderList, addOrder } from "@/common/apis/order";
import {
  addPayOrder,
  aftercareDetail,
  aftercareList,
  applyAftercare,
  beforeCreateOrder,
  cancelAftercare,
  cancelOrder,
  cancelWechatPayOrder,
  orderDetail,
  orderHistory,
  payOrderStatus,
} from "../common/apis/order";

export default {
  state() {
    return {
      selectGoodsList: uni.getStorageSync("selectGoodsList")
        ? uni.getStorageSync("selectGoodsList")
        : [],
    };
  },

  getters: {},

  mutations: {
    setSelectOrdersList(state, data) {
      state.selectGoodsList = data;
      uni.setStorageSync("selectGoodsList", state.selectGoodsList);
    },
  },

  actions: {
    // 获取订单列表
    async fetchOrderList({ state }, payload) {
      const response = await orderList(payload);
      if (response) {
        state.list = response;
      }
      return response;
    },
    // 获取订单详情
    async fetchOrderDetail({ state }, payload) {
      const response = await orderDetail(payload);
      if (response) {
        return Promise.resolve(response);
      }
      return Promise.reject(response);
    },
    // 添加订单
    async fetchAddOrder({ dispatch }, payload) {
      const response = await addOrder(payload);
      if (response) {
        dispatch("fetchOrderList");
      }
      return response;
    },
    // 创建支付订单
    async fetchAddPayOrder({}, payload) {
      const response = await addPayOrder(payload);
      return response;
    },
    // 取消支付订单
    async fetchCancelWechatPayOrder({}, payload) {
      const response = await cancelWechatPayOrder(payload);
      return response;
    },
    // 取消订单
    async fetchCancelOrder({}, id) {
      const response = await cancelOrder(id);
      return response;
    },
    // 申请售后
    async fetchApplyAftercare({}, payload) {
      const response = await applyAftercare(payload);
      if (response) {
        return Promise.resolve(response);
      }
      return Promise.reject(response);
    },
    // 售后单列表
    async fetchAftercareList({}, payload) {
      const response = await aftercareList(payload);
      if (response) {
        return Promise.resolve(response);
      }
      return Promise.reject(response);
    },
    // 售后单详情
    async fetchAftercareDetail({}, id) {
      const response = await aftercareDetail(id);
      if (response) {
        return Promise.resolve(response);
      }
      return Promise.reject(response);
    },
    // 取消售后
    async fetchCancelAftercare({}, id) {
      const response = await cancelAftercare(id);
      if (response) {
        return Promise.resolve(response);
      }
      return Promise.reject(response);
    },
    // 历史订单
    async fetchOrderHistory({}, payload) {
      const response = await orderHistory(payload);
      if (response) {
        return Promise.resolve(response);
      }
      return Promise.reject(response);
    },
    // 支付结果
    async fetchPaymentStatus({}, payload) {
      const response = await payOrderStatus(payload);
      if (response) {
        return Promise.resolve(response.data.status);
      }
      return Promise.reject(response);
    },
    // 预下单
    async fetchBeforeOrderCreate({}, payload) {
      const response = await beforeCreateOrder(payload);
      if (response) {
        return Promise.resolve(response.data);
      }
      return Promise.reject(response);
    },
  },
};
